package com.hpxx.ylzswl.base;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hpxx.ylzswl.R;
import com.umeng.analytics.MobclickAgent;
import com.universal.frame.generalutils.ExitAppUtils;
import com.universal.frame.swipeback.SwipeBackActivityBase;
import com.universal.frame.swipeback.SwipeBackActivityHelper;
import com.universal.frame.swipeback.SwipeBackLayout;
import com.universal.frame.swipeback.Utils;

import java.io.IOException;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

public abstract class BaseActivity extends FragmentActivity implements SwipeBackActivityBase {

    private SwipeBackActivityHelper mHelper;

    private SwipeBackLayout mSwipeBackLayout;

    public PopupWindow popupWindow = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        this.initSwipeBackLayout();
        ExitAppUtils.getInstance().addActivity(this);
        MobclickAgent.setDebugMode(true);
    }

    public void initStateTab() {
        int version = Build.VERSION.SDK_INT;
        /**当前手机的版本在>=19但<21**/
        if (!hasLollipop()) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明底部导航栏
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        /**当前手机的版本>=21的时候**/
        else if (hasLollipop()) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            /**获得顶层视图**/
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            /**为华为荣耀专项设置**/
            if ("HONOR".equals(Build.BRAND)) {
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
            window.setStatusBarColor(Color.TRANSPARENT);
            Log.i("====", Build.BRAND + "");
        }
    }

    public static boolean hasKitKat() {
        /**Build.VERSION_CODES.KITKAT 代表API版本19 即4.4**/
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    }

    public static boolean hasLollipop() {
        /**Build.VERSION_CODES.LOLLIPOP 代表API版本21 即5.0**/
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }

    private void initSwipeBackLayout() {
        this.mHelper = new SwipeBackActivityHelper(this);
        this.mHelper.onActivityCreate();
        this.mSwipeBackLayout = getSwipeBackLayout();
        this.mSwipeBackLayout
                .addSwipeListener(new SwipeBackLayout.SwipeListener() {

                    @Override
                    public void onScrollStateChange(int state,
                                                    float scrollPercent) {
                        Log.i("taglv_tag", "taglv_tag = " + 1);

                    }

                    @Override
                    public void onEdgeTouch(int edgeFlag) {
                        Log.i("taglv_tag", "taglv_tag = " + 2);
                    }

                    @Override
                    public void onScrollOverThreshold() {
                        Log.i("taglv_tag", "taglv_tag = " + 3);
                    }

                });
    }

    public abstract void initView();

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        try{
            initView();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        try{
            initView();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        ExitAppUtils.getInstance().delActivity(this);
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        this.mHelper.onPostCreate();
    }

    @Override
    public View findViewById(int id) {
        View v = super.findViewById(id);
        if (v != null)
            return v;
        return this.mHelper.findViewById(id);
    }


    @Subscribe
    public void onEventMainThread(BaseEvent event) throws IOException {

    }

    @Override
    public SwipeBackLayout getSwipeBackLayout() {
        // TODO Auto-generated method stub
        return this.mHelper.getSwipeBackLayout();
    }

    @Override
    public void setSwipeBackEnable(boolean enable) {
        // TODO Auto-generated method stub
        getSwipeBackLayout().setEnableGesture(enable);
    }

    @Override
    public void scrollToFinishActivity() {
        // TODO Auto-generated method stub
        Utils.convertActivityToTranslucent(this);
        getSwipeBackLayout().scrollToFinishActivity();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }


    /**
     * 返回
     */
    public void back(){
        findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * 设置标题栏
     */
    public void setTitle(String title){
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(title);
    }

    /**
     * 设置右标题点击事件
     * @param linster
     */
    public void reghtLinster(View.OnClickListener linster){
        findViewById(R.id.ll_reght).setOnClickListener(linster);
    }


    /**
     * 设置吧右标题
     * @param title
     */
    public void setReghtTitle(String title){
        TextView tv_reght = (TextView) findViewById(R.id.tv_reght);
        tv_reght.setText(title);
    }

    /**
     * 设置有标题背景
     */
    public void setBackground(int resource){
        TextView tv_reght = (TextView) findViewById(R.id.tv_reght);
        tv_reght.setBackgroundResource(resource);
    }

    public PopupWindow getPopupWindow(View popupWindowView) {
        if (null == popupWindow) {
            popupWindow = new PopupWindow(popupWindowView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
//        popupWindow.setBackgroundDrawable(getResources().getDrawable(
//                R.drawable.menu_background));
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.PopupWindowInAndOutStyle);
        popupWindow.update();
        return popupWindow;
    }

}
